/**
 * 递归处理对象或数组
 * @param object 处理对象
 * @param callback 回调函数
 * @param visited 循环引用记录器
 * @param path 属性路径
 **/
const deepHandle = <T>(
  object: T,
  callback: (currentPath: string[], value: any, object: T) => void,
  visited = new WeakSet(),
  path = [],
): T => {
  // 检测基本类型
  if (object === null || typeof object !== 'object') return object

  // 检测循环引用
  if (visited.has(object)) return object
  visited.add(object)

  // 对象处理
  const keys = Object.keys(object)
  for (const key of keys) {
    const value = object[key]
    const currentPath = [...path, key]

    // 执行回调
    callback(currentPath, value, object)

    // 递归处理对象/数组
    if (value !== null && typeof value === 'object') {
      deepHandle(value, callback, visited, currentPath)
    }
  }

  return object
}
